var $_GET = (function() {
    var url = window.document.location.href.toString();
    var u = url.split("?");
    if (typeof(u[1]) == "string") {
        u = u[1].split("&");
        var get = {};
        for (var i in u) {
            var j = u[i].split("=");
            get[j[0]] = j[1];
        }
        return get;
    } else {
        return {};
    }
})();

var toastTimeout;

function toast(msg) {
    if (toastTimeout) {
        clearTimeout(toastTimeout);
    }
    if ($('body>.toast').length === 0) {
        $('body').append('<div class="toast"></div>');
    }

    var toastBox = $('body>.toast');

    toastBox.html(msg);

    //延迟一帧执行加show class 延迟执行css动画渲染
    setTimeout(function() {
        var boxPadding = toastBox.css('paddingLeft');
        var boxWidth = toastBox.width();
        toastBox.css('marginLeft', -boxWidth/2 - boxPadding.slice(0,boxPadding.length -2));
        toastBox.css('left', '50%');
        toastBox.addClass('show');
    }, 1);
    toastTimeout = setTimeout(function() {
        toastBox.removeClass('show');
    }, 2000);
}

//发送验证码时添加cookie
function addCookie(name, value, expiresHours) {
    var cookieString = name + "=" + escape(value);
    //判断是否设置过期时间,0代表关闭浏览器时失效
    if (expiresHours > 0) {
        var date = new Date();
        date.setTime(date.getTime() + expiresHours * 1000 * 3600);
        cookieString = cookieString + ";expires=" + date.toUTCString();
    }
    document.cookie = cookieString;
}
//修改cookie的值
function editCookie(name, value, expiresHours) {
    var cookieString = name + "=" + escape(value);
    if (expiresHours > 0) {
        var date = new Date();
        date.setTime(date.getTime() + expiresHours * 1000 * 3600); //单位是毫秒
        cookieString = cookieString + ";expires=" + date.toGMTString();
    }
    document.cookie = cookieString;
}
//根据名字获取cookie的值
function getCookieValue(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}

//节流函数
function throttle(method, context, time) {
    clearTimeout(method.tId);
    method.tId = setTimeout(function() {
        method.call(context);
    }, time);
}

//模板渲染
function formate(val, formation) {
    var output = formation;
    for(var i in val) {
        var output = output.replace(RegExp('%' + i, 'g'), val[i]);
    }
    return output
}

function customBack(href) {
    var state = {
        customBack: true
    };
    setTimeout(function() {
        window.history.pushState(state, "title", "#1");
        window.addEventListener("popstate", function(e) {
            if(e.state.customBack) {
                window.location.href = href;
            }
        }, false);
    }, 1000);
}
function hideFix(eles) {
    //解决ios呼出输入法时fix失效的问题
    var u = navigator.userAgent, app = navigator.appVersion;
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //android终端或者uc浏览器
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    var fixEles = $(eles);
    if(fixEles.length == 0 || !isiOS) {
        return false;
    }
    $('body').on('focus', 'textarea, input, select', function() {
        fixEles.hide();
    });
    $('body').on('blur', 'textarea, input, select', function() {
        fixEles.show();
    });
}
//获取url参数
function getQueryParams(qs) {
    qs = qs.split('+').join(' ');
    var params = {},
        tokens,
        re = /[?&]?([^=]+)=([^&]*)/g;
    while (tokens = re.exec(qs)) {
        params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
    }
    return params;
}
